package com.zy.experience_system.controller;

import com.zy.experience_system.enums.PermsEnum;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.ClassAndSetForm;
import com.zy.experience_system.form.ClassInformationForm;
import com.zy.experience_system.service.ClassService;
import com.zy.experience_system.util.ResultUtil;
import com.zy.experience_system.vo.ClassAdminVO;
import com.zy.experience_system.vo.ClassCountVO;
import com.zy.experience_system.vo.ResultVO;
import com.zy.experience_system.vo.StuInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/class")
public class ClassController extends BaseController {

    @Autowired
    private ClassService classService;

    @PostMapping("/create")
    public ResultVO create(@Validated @RequestBody ClassInformationForm classInformationForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                Integer classId = classService.save(classInformationForm);
                Map<String, Integer> map = new HashMap<>();
                map.put("classId", classId);
                return ResultUtil.success(map);
            } catch (Exception e) {
                return ResultUtil.error("创建失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @PutMapping("/updateName")
    public ResultVO updateName(@Validated @RequestBody ClassInformationForm classInformationForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            if (classInformationForm.getClassId() != null && classInformationForm.getClassName() != null) {
                try {
                    classService.save(classInformationForm);
                    return ResultUtil.success(null);
                } catch (Exception e) {
                    return ResultUtil.error("修改失败");
                }
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/allOfTeacher/{teachId}")
    public ResultVO allOfTeacher(@PathVariable("teachId") String teachId) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            if (teachId != null) {
                try {
                    List<ClassCountVO> classCountVOList = classService.findAllByTeach(teachId);
                    return ResultUtil.success(classCountVOList);
                } catch (Exception e) {
                    return ResultUtil.error("查询失败");
                }
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/all")
    public ResultVO all() throws Exception {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                List<ClassAdminVO> classAdminVOList = classService.findAll();
                return ResultUtil.success(classAdminVOList);
            } catch (Exception e) {
                return ResultUtil.error("查询失败");
            }

        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @DeleteMapping("/delete/{classId}")
    public ResultVO delete(@PathVariable("classId") Integer classId) throws Exception {
        if (!claims.get("perms").equals(PermsEnum.STUDENT.getPerms())) {
            try {
                classService.delete(classId);
                return ResultUtil.success(null);
            }catch (Exception e){
                return ResultUtil.error("删除失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @PostMapping("/addToSet")
    public ResultVO addToSet(@Validated @RequestBody ClassAndSetForm classAndSetForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                classService.save(classAndSetForm);
                return ResultUtil.success(null);
            }catch (Exception e){
                return ResultUtil.error("添加失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @DeleteMapping("/deleteFromSet")
    public ResultVO deleteFromSet(@Validated @RequestBody ClassAndSetForm classAndSetForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                classService.delete(classAndSetForm);
                return ResultUtil.success(null);
            }catch (Exception e){
                return ResultUtil.error("删除失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }


    @GetMapping("/allOfSet/{setId}")
    public ResultVO allOfSet(@PathVariable("setId") Integer setId) throws Exception{
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            if (setId != null) {
                try {
                    List<ClassCountVO> classCountVOList = classService.findAllBySet(setId);
                    return ResultUtil.success(classCountVOList);
                } catch (Exception e) {
                    return ResultUtil.error("查询失败");
                }
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }



}
